/***
 * @description: 组合商品修改发货渠道弹框
 * @author:elvis
 */
import React, { useState, useEffect } from "react";
import CommonModal from '@/components/CommonModal';
import { Form, message, Select } from 'antd';
import CommonLoading from '@/components/CommonLoading';
import { getCurrentUser } from '@/utils/authority';
import { setChannel, queryTypeChannel } from '../service';

const { Option } = Select;

//获取渠道类型
const getChannelType = (arr) => {
	const list = arr.map(v => v.deliverType);
	if (list.includes(3)) {
		if (list.includes(4)) {
			return null;
		} else {//库存加拿货
			return 1;
		}
	} else {
		if ((list.includes(1) || list.includes(2)) && !list.includes(4)) {
			let btn = false;
			for (let i in arr) {
				if (arr[i].isCarefullySelected != 1) {
					btn = true;
					break;
				}
			}
			if (btn) {
				return 8;
			} else {
				return 7;
			}
		} else {//都是供应商直发
			let btn = false;
			for (let i in arr) {
				if (arr[i].zjfStoreId != arr[0].zjfStoreId || arr[i].storeId != arr[0].storeId) {
					btn = true;
					break;
				}
			}
			if (btn) {//
				return null;
			} else {
				return 0;
			}
		}
	}
}

export default ({ item, visible, handleOk, handleCancel }) => {

	const [form] = Form.useForm();

	const [loading, setLoading] = useState(false);

	//下拉渠道列表，需要根据type请求不同的list
	const [channelList, setChannelList] = useState([]);

	//获取渠道下拉列表
	const getChannelList = async () => {
		const type = getChannelType(item.composeGoodsDetails);
		if (type) {
			setLoading(true);
			const { data, success } = await queryTypeChannel({
				isDeleted: 0,
				type,
				uuid: getCurrentUser().uuid,
			})
			setLoading(false);
			if (success) {
				setChannelList(data)
			}
		} else {
			if (type == 0) {
				const arr = [
					{ id: 0, channelName: `${item.composeGoodsDetails[0].zjfStoreName}(直发)` }
				]
				setChannelList(arr);
			}
		}

	}

	useEffect(() => {
		if (visible) {
			getChannelList();
		}
	}, [visible])

	const onOk = () => {
		form.validateFields().then(val => {
			updateChannel(val);
		}).catch(err => {
			console.log(err, 'err');
		})
	}

	const updateChannel = async (val) => {
		setLoading(true);
		const { success } = await setChannel({
			...val,
			id: item.id,
		})
		if (success) {
			message.success('渠道设置成功');
			handleOk();
		}
		setLoading(false);
	}

	const optionChannel = channelList.map(item => {
		return <Option key={item.id} value={item.id}>{item.channelName}{ item.id == 0 ? null : getCurrentUser().uuid == item.uuid ? '(自发)' : '(第三方)'}</Option>
	})

	return (
		<>
			<CommonModal
				title="修改发货渠道"
				visible={visible}
				onCancel={handleCancel}
				onOk={onOk}
				width='460px'
			>
				<div className="form_box">
					<Form
						name="edit-channel"
						form={form}
						initialValues={{
							channelId: item.channelId
						}}
					>
						<Form.Item
							label="发货渠道"
							name="channelId"
							rules={[
								{ required: true, message: '请选择发货渠道' },
							]}
						>
							<Select size='small' placeholder="请选择发货渠道" style={{ width: '260px' }}>
								{optionChannel}
							</Select>
						</Form.Item>
					</Form>
				</div>
			</CommonModal>
			<CommonLoading loading={loading} />
		</>
	)
}
